# -*- coding: utf-8 -*-
"""
Created on Tue Feb 26 11:10:27 2013
"""
from aircraft import aircraft
import paths
import dbTools
import numpy as np

def load(name):
    pth=paths.Database()
    acDB =dbTools.loadDB(pth.aircraft)
    return Design(acDB.selectByName(name))

class Design(aircraft):
    """
    Class providing tools for easy modification of aircraft geometry. All 
    methods of aircraft class are inherited (mass, drag etc.).
    Mass and parasite drag are updated automatically whenever changes are made 
    that can influence on it. 
    
    Examples
    --------
    
    Loading aircraft geometry is similar to aircraft.py
    
    >>> ac = load('V0510')
    >>> ac.display()
    
    Changing position of the wing
    
    >>> ac.set_wing_position([2.0,1.0,1.0])
    >>> ac.display()

    Setting mass of fuel
    
    >>> ac.mass.fuel.set_fuel_mass(15.0,'total')
    >>> ac.display()
    
    Changing position of vertical tail
    
    >>> ac.set_vstab_position([5.0,0.0,0.3])
    >>> print ac.vStab.centroid
    [ 5.81488667  0.          1.00416667]
    >>> ac.display()
    """
    def set_wing_position(self,aapex):
        """
        Changes main wing aapex position
        
        Parameters
        ----------
        
        aapex : 3d float
            wing aapex position in meters [x,y,z]
        """
        self.wing.aapex = np.array(aapex)
        self._upd_wing()
        
    def set_wing_chord_by_index(self,chord,idx):
        """
        Changes wing chord specified by index
        
        Parameters
        ----------
        
        chord : float, m
            chord length
        idx : integer
            index of the chord to be changed
        """
        self.wing.chords[idx] = float(chord)
        self._upd_wing()
    def set_wing_chords(self,chords):
        """
        Changes all chords of the wing.
        
        Parameters
        ----------
        
        chords : float array, m
            array of chord lengths
        """
        self.wing.chords = np.array(chords)
        self._upd_wing()
        
    def set_wing_spans(self,spans):
        """
        Changes wing segment spans
        
        Parameters
        ----------
        
        spans : float array, m
            array of segment spans
        """
        self.wing.segmentSpans = np.array(spans)
        self._upd_wing()

    def set_wing_airfoils(self,airfoils):
        pass
    def set_wing_dihedrals(self,dihedrals):
        """
        Set dihedral angles of wing segments
        
        Parameters
        ----------
        
        dihedrals : float array, m
            array of dihedral angles
        """
        self.wing.segmentDihedrals = np.array(dihedrals)
        self.wing.segmentDihedrals_rad = np.deg2rad(dihedrals)
        print self.wing.segmentDihedrals_rad
        self._upd_wing()
    def set_wing_sweep(self,sweep):
        pass
    def set_wing_twist(self,twist):
        pass
    def set_wing_incidence(self,incidence):
        self.wing.incidence = incidence
        self._upd_wing()
    def scale_wing_span(self,newSpan):
        scale = float(newSpan) / self.wing.span
        self.wing.segmentSpans *= scale
        self._upd_wing()
    def scale_wing_chord(self,newRootChord):
        pass
    def update_fuel_mass(self,fuelMass):
        """
        Updates mass of the fuel in wing. Wing is distributed symmetrically 
        in both fuel tanks preserving same span and chord ratio location of 
        the fuel tank
        
        Parameters
        ----------
        
        fuelMass : float, kg
            new mass of the fuel
        """
        spanRatio = self.wing.fuelTankCGspanRatio
        chordRatio = self.wing.fuelTankCGchordRatio
        self.wing.setFuelTank(fuelMass,1,spanRatio,chordRatio)
    def update_fuel_tank_position(self,spanRatio,chordRatio):
        fuelMass = self.designGoals.fuelMass
        self.wing.setFuelTank(fuelMass,1,spanRatio,chordRatio)
        self._upd_wing()
    def _upd_wing(self):
        """
        updates all parameters of the wing
        """
        self.wing._getWingData()
        #fuelMass = self.designGoals.fuelMass/2.0
        #self.update_fuel_mass(fuelMass)
        self.update_mass()
        self.update_drag()
    def _upd_hstab(self):
        self.hStab._getWingData()
        self.update_mass()
        self.update_drag()
    def _upd_vstab(self):
        self.vStab._getWingData()
        self.update_mass()
        self.update_drag()
    def set_hstab_position(self,aapex):
        self.hStab.aapex = np.array(aapex)
        self._upd_hstab()
    def set_hstab_chords(self,chords):
        self.hStab.chords = np.array(chords)
        self._upd_hstab()
    def set_hstab_chord_by_index(self,chord,idx):
        self.hStab.chords[idx] = float(chord)
        self._upd_hstab()
    def set_hstab_spans(self,spans):
        self.hStab.segmentSpans = np.array(spans)
        self._upd_hstab()
    def set_vstab_position(self,aapex):
        self.vStab.aapex = np.array(aapex)
        self._upd_vstab()
    def set_vstab_chord_by_index(self,chord,idx):
        self.vStab.chords[idx] = float(chord)
        self.vStab.vertical = True
        self._upd_vstab()
    def set_vstab_chords(self,chords):
        self.vStab.chords = np.array(chords)
        self._upd_vstab()
    def set_vstab_spans(self,spans):
        self.vStab.segmentSpans = np.array(spans)
        self._upd_vstab()
    def set_vstab_span_by_index(self,span,idx):
        self.vStab.segmentSpans[idx] = float(span)
        self._upd_vstab()
    def scale_vstab_span(self,newSpan):
        scale = float(newSpan)/self.vStab.span
        self.vStab.segmentSpans *= scale
        self._upd_vstab()
    def set_vstab_offset(self,offsets):
        self.vStab.segmentOffsets = np.array(offsets)
        self._upd_vstab
    def set_vstab_offset_by_index(self,offset,idx):
        self.vStab.segmentOffsets[idx] = offset
        self._upd_vstab()

def run_test1():
    ac = load('V0510')
    ac.display()
    ac.set_wing_position([2.0,1.0,1.0])
    ac.display()
    ac.mass.fuel.set_fuel_mass(15.0,'total')
    ac.display()
    ac.set_vstab_position([5.0,0.0,0.3])
    print ac.vStab.centroid
    ac.display()
    ac.set_vstab_position([4.0,0.0,1.0])
    print ac.vStab.centroid
    ac.display()
    ac.set_vstab_position([4.0,0.0,-0.3])
    ac.display()

if __name__=="__main__":
    run_test1()